/*
 * Copyright (C) 2012 Tuomas Kulve <tuomas@kulve.fi>
 * Copyright (C) 2010-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense,     
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS  
 * IN THE SOFTWARE. 
 */


#include "cmdmethod.h"

#include <QDebug>
#include <QProcess>
#include <QFile>
#include <MBanner>
#include <ShareUI/ItemContainer>
#include <ShareUI/DataUriItem>
#include <ShareUI/FileItem>
#include <GConfItem>

CmdMethod::CmdMethod(QObject * parent) : ShareUI::MethodBase(parent)
{
}

CmdMethod::~CmdMethod()
{
}

QString CmdMethod::title()
{
  QString name = qtTrId ("Command-line Sharing");
  return name;
}

QString CmdMethod::icon()
{
  return QString::fromLatin1 ("icon-cmdshare");
}

QString CmdMethod::id()
{
  return QString::fromLatin1("com.meego.cmdshare");
}

void CmdMethod::selected(const ShareUI::ItemContainer *items)
{
    
  QStringList filePaths;
  QString body;
  QString subject;
  QString cmd;
  ShareUI::SharedItem item;

  ShareUI::ItemIterator itemsIter = items->itemIterator();
  while (itemsIter.hasNext()) {
    
	item = itemsIter.next();
                    
	GConfItem* gi = new GConfItem("/apps/ControlPanel/CmdShare/cmd", this);
	cmd = gi->value().toString();

	ShareUI::FileItem * fileItem = qobject_cast<ShareUI::FileItem*> (item.data());
                
	if (fileItem != 0) {
	  filePaths << fileItem->filePath();
	}
  }
  
  // Add files
  QStringList files;
  for (int i = 0; i < filePaths.count(); ++i) {
	files << filePaths.at (i);
  }

  // Replace %s with the first file
  cmd = cmd.replace("%s", files.at(0));

  if (1) { // Debug
	QFile file("/tmp/out.txt");
	file.open(QIODevice::WriteOnly | QIODevice::Text);
	QTextStream out(&file);
	out << "files:" << files.join(" ") << "\n";
	out << "cmd:" << cmd << "\n";
	file.close(); 
  }

  // Launch the command
  QProcess process;
  if (!QProcess::startDetached(cmd)) { 

	// Failed to launch the command
	QString err = "Failed to launch: " + cmd;
	qCritical(err.toLatin1());
	emit(selectedFailed(err));
  } else {
	// Command successfully launched (still running)
	emit(done());
  }
}

void CmdMethod::currentItems (const ShareUI::ItemContainer * items)
{
  Q_EMIT(visible(acceptContent(items)));
}

bool CmdMethod::acceptContent (const ShareUI::ItemContainer * items)
{

  if (items == 0 || items->count() == 0) {
	return false;
  }

  GConfItem* gi = new GConfItem("/apps/ControlPanel/CmdShare/enabled", this);
  bool enabled = gi->value().toBool();

  if (!enabled) {
	return false;
  }

  ShareUI::ItemIterator itemsIter = items->itemIterator();

  // Limit to files
  while (itemsIter.hasNext()) {

	ShareUI::SharedItem item = itemsIter.next();
	ShareUI::FileItem * fileItem = ShareUI::FileItem::toFileItem (item);

	if (fileItem == 0) {
	  return false;
	}
  }

  return true;
}
